package com.maple.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;
import org.apache.struts2.ServletActionContext;
public class ServletUtilsw {
	public static String gainParameter(String param)
	{
		if(param==null)
			return null;
        HttpServletRequest request = ServletActionContext.getRequest();
        String value=request.getParameter(param);
        return value;
	}
	
	public static HttpServletRequest gainRequest()
	{
		return ServletActionContext.getRequest();
	}
	
	public static HttpServletResponse gainResponse()
	{
		return ServletActionContext.getResponse();
	}
	
	
	
	
	public static HashMap toEntityMap(HttpServletRequest request) {
		HashMap map=new HashMap();
		Enumeration e= request.getParameterNames();
		 while(e.hasMoreElements())
		 {
			 String k=(String) e.nextElement();
			 map.put(k, request.getParameter(k));
		 }
		 return map;
	}
//	public static <T> T populate(T entity,Map map)
	public static <T> T populate(HttpServletRequest request,T entity)
	{
        
		HashMap map=ServletUtilsw.toEntityMap(request);
		
        ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
        convertUtilsBean.register(new Converter() {
            public Object convert(Class type, Object value) {
                if(value==null||"".equals(value)) 
                    return null;
                if(!value.getClass().equals(String.class))
                {
                    throw new ConversionException("日期转换器只支持String类型的转");
                }
                SimpleDateFormat sdf=null;
                String source=(String) value;
                source=source.trim();
                if(source.length()>10)
                    sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                else
                    sdf=new SimpleDateFormat("yyyy-MM-dd");
                
                try {
                    return  new Timestamp(sdf.parse(source).getTime());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }, Timestamp.class);
        
        BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean);
        try {
            beanUtilsBean.populate(entity, map);
            
        } catch (Exception e) {
        	 throw new RuntimeException(e);
        } 
		return entity;
	}
	@SuppressWarnings("unchecked")
	public static <T> String gainJsonStr(T t)
	{
		String json=JSONObject.fromObject(t).toString();
		return json;
	}
	@SuppressWarnings("unchecked")
	public static <T> void writeJsonStr(T t)
	{
		String json=gainJsonStr(t);
		try {
			HttpServletResponse response= ServletUtilsw.gainResponse();
			PrintWriter writer =response.getWriter();
			writer.write(json);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	
	public static String extractStrFromReqBody(HttpServletRequest request) throws IOException {
		BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
		String jsonStr="";
		char[] cbuf=new char[1024];
		int len=0;
		while((len=reader.read(cbuf))!=-1)
		{
			jsonStr+=new String(cbuf,0,len);
		}
		return jsonStr;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
